<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return['ischecklogin']=$this->model('member')->get_member();
//购买会员
if($_RQ['op']=='get'){
	set_html('title','购买会员');
	$member['nextlevel']=pdo_get('jy_ailiao_level',array('uniacid'=>$_SESSION['uniacid'],'type'=>1,'level >'=>$member['level']['level']),'','level asc');
	$level_id=$member['nextlevel']['id'];
	$level=pdo_get('jy_ailiao_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$level_id));
	$post=array();
	$post['level_id']=$level_id;
	$post['paytype']=1;
	$post['num']=1;
	$post['checked']=true;
	$return['systemxieyi']=$this->plugin_setting('xieyi');
	$return['post']=$post;
	$return['fee']=price($level['fee_open1']);
	$return['level']=$level;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$level_id=$postdata['level_id'];
	$level=pdo_get('jy_ailiao_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$level_id));
	if($postdata['paytype']==1){
		$price=$level['fee_open1'];
	}elseif($postdata['paytype']==2){
		$price=$level['fee_open2'];	
	}elseif($postdata['paytype']==3){
		$price=$level['fee_open'];
	}
	if($price>0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'level_id'=>$level_id,
			'sn'=>get_ordersn('jy_ailiao_viporder','sn'),
			'price'=>$price,
			'type'=>$postdata['paytype'],
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_ailiao_viporder',$insert);
		$id=pdo_insertid();
			$params=array(
					'id'=>$id,
					'title'=>'开通VIP会员',
					'fee'=>$price,
					'plugin'=>'jy_ailiao',
					'no_type'=>array('hdfk'),
					'back_url'=>'jy_ailiao/member.buyvip/payresult/',
					'success_url'=>'jy_ailiao/index/',
					'error_url'=>'jy_ailiao/index/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_ailiao_viporder',array('uniacid'=>$_SESSION['uniacid'],'payid'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
		exi('付费失败，价格未设置！');
	}
}
if($_RQ['op']=='setpay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_ailiao_viporder',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$res=array(
		'pay_id'=>$order['payid'],
		'order_id'=>$order['id'],
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_ailiao_viporder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_ailiao_viporder',array('ispay'=>1,'status'=>1,'paytype'=>$payresult['pay_type'],'transid'=>$payresult['transaction_id']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	$next_level=pdo_get('jy_ailiao_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['level_id']));
	$update=array(
		'level_id'=>$next_level['id'],
	);
	if($next_level['pingnum']){
		$update['pingnum +=']=$next_level['pingnum'];
	}
	if($next_level['siminum']){
		$update['siminum +=']=$next_level['siminum'];
	}
	if($order['type']==1){
		$month=1;
	}elseif($order['type']==2){
		$month=3;
	}elseif($order['type']==3){
		$month=12;
	}
	$member=pdo_get('jy_ailiao_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	if(!$member['viptime']){
		$update['viptime']=time()+$month*30*24*3600;
	}else{
		$update['viptime +=']=$month*30*24*3600;
	}
	pdo_update('jy_ailiao_member',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	$order['money']=$order['price'];
	$this->model('commission')->jy_teamfee($order,'购买相亲VIP');
	$this->model('commission')->jy_area_agent($order,'购买相亲VIP');
	$this->model('commission')->jy_commission($order,'购买相亲VIP');
	exi('支付成功','success','jy_ailiao/index/');
}